

from ninja import ModelSchema, Router, Schema

from customauth.models import MyUser
from .models import Spr_otd

router = Router()


class Error(Schema):
    message: str


class MyUserSchema(Schema):
    login: str
    mpp: int
    fam: str
    im: str
    ot: str
    is_active: bool
    is_admin: bool


@router.get(
    "/",
    response={200: list[MyUserSchema], 403: Error(message="Ошибка обработки запроса")},
)
def list_events(request) -> list[MyUserSchema]:
    return list(MyUser.objects.all())

# class LpuSchema(ModelSchema):
#     class Meta:
#         model = Spr_lpu
#         fields = "__all__"

# class OtdSchema(ModelSchema):
#     otd: int
#     name_otd: str
#     name_otdkr: str
#     lpu: LpuSchema
#     type_otd: int
#     dred: date
#     is_active: bool

# OtdSchema = create_schema(Spr_otd, fields=["otd", "name_otd"])

class OtdSchema(ModelSchema):
    class Meta:
        model=Spr_otd
        fields = "__all__"


@router.get("/list_otd", response={200: OtdSchema | list[OtdSchema], 403: Error(message="Ошибка обработки запроса")})
def list_otd(request, otd=0):
    print(otd)
    if otd == 0:
        return [x for x in Spr_otd.objects.all()]
    return Spr_otd.objects.filter(otd=otd).first()
